//
//  ComputerPlayer.m
//  BetsAndBrains
//
//  Created by Augustine, Eric M (augus011) on 4/16/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "ComputerPlayer.h"
#import <stdlib.h>
#import "Guess.h"
#import "Game.h"
#import "Bet.h"


@implementation ComputerPlayer

-(id)initWithName:(NSString*)name{
	self = [super initWithName:name];
	return self;
}

-(BOOL)randomBool{
	u_int32_t randomNumber = (arc4random() % ((unsigned)RAND_MAX + 1));
	if(randomNumber % 5 ==0){
		return YES;
	}
	return NO;	
}

-(void)submitGuess:(NSInteger)answer{
	NSInteger number = 0;
	NSInteger deviation = 0;
	BOOL b = [self randomBool];
	
	if(answer < 10){
		deviation = 5;
	} else if (answer < 100){
		deviation = 50;
	}else{
		deviation = 100;
	}
	
	if(b){
		number = answer + ((arc4random() % deviation) + 1);
	}else{
		number = answer - ((arc4random() % deviation) + 1);
		if(number < 0){
			number = -number;
		}
	}
	
	Guess* g = [[Guess alloc] initWithPlayerName:self.playerName andGuess:number];
	[self.game receiveGuess:g];
	[g autorelease];
}

-(void)chooseBet{
	NSInteger choice = 0;
	NSMutableArray* betList= self.game.bets;
	for(NSInteger i = 0; i < betList.count; i++){
		choice += [[betList objectAtIndex:i] choice];
	}
	
	choice = round(choice / betList.count);
	
	int chips = self.permChips + self.gameChips;
	
	[self placeBet:choice :chips];
}

@end
